package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.view.View;
import android.view.ViewGroup;

import com.jwdf.uda.R;
import com.jwdf.uda.bean.UploadImage;
import com.jwdf.uda.databinding.ItemNewVoteImageBinding;
import com.jwdf.uda.databinding.ItemNewVoteTextBinding;
import com.jwdf.uda.ui.fragment.question.bean.NewVote;
import com.jwdf.uda.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 类说明：
 */
public class NewVoteDetailAdapter extends BaseAdapter<NewVote>{
    private List<NewVote> mItems = new ArrayList<>();
    private Context mContext;
    private static final int ITEM_TEXT = 0;
    private static final int ITEM_IMAGE = 1;

    public NewVoteDetailAdapter(Context context) {
        super(context);
        mContext = context;
    }

    public void setDetailData(List<NewVote> newVoteList) {
        mItems.clear();

//        addUserItem(newVoteList);
        try {
            if (newVoteList.get(0).getDescription().length()>0) {
                addPlanImageItem(mContext.getString(R.string.vote_plan_title_a),newVoteList.get(0).getUploadImages(),newVoteList.get(0).getDescription() );
            }
            if (newVoteList.get(1).getDescription().length()>0) {
                addPlanImageItem(mContext.getString(R.string.vote_plan_title_b), newVoteList.get(1).getUploadImages(),newVoteList.get(1).getDescription());
            }
            if (newVoteList.get(2).getDescription().length()>0) {
                addPlanImageItem(mContext.getString(R.string.vote_plan_title_c), newVoteList.get(2).getUploadImages(),newVoteList.get(2).getDescription());
            }
            if (newVoteList.get(3).getDescription().length()>0) {
                addPlanImageItem(mContext.getString(R.string.vote_plan_title_d), newVoteList.get(3).getUploadImages(),newVoteList.get(3).getDescription());
            }
        }catch (Exception e){

        }

        append(mItems);
        notifyDataSetChanged();
    }


//    private void addUserItem(List<NewVote> newVoteList) {
//        NewVote item = new NewVote();
//        item.setUploadImages(newVoteList.get());
//        mItems.add(item);
//    }

    private void addPlanImageItem(String planName, List<UploadImage> images, String title) {
        NewVote item = new NewVote();
        item.setDescription(title);
        item.setUploadImages(images);
        item.setTitle(planName);
        mItems.add(item);

    }

    public int getItemViewType(int position) {
        if(mItems.get(position).getUploadImages().size()>0){
            return ITEM_IMAGE;
        }else {
            return ITEM_TEXT;
        }
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int itemType) {
        switch ((itemType)) {
            case ITEM_TEXT:
                return new VotePlanTextViewHolder(mInflater.inflate(R.layout.item_new_vote_text, parent, false));
            case ITEM_IMAGE:
                return new VotePlanImageViewHolder(mInflater.inflate(R.layout.item_new_vote_image, parent, false));
            default:
                return new VotePlanTextViewHolder(mInflater.inflate(R.layout.item_new_vote_text, parent, false));
        }

    }

    public class VotePlanImageViewHolder extends BaseViewHolder<NewVote>{
        private ItemNewVoteImageBinding mItemBinding;
        public VotePlanImageViewHolder(View convertView) {
            super(convertView);
            mItemBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(NewVote item) {
            mItemBinding.tvPlanName.setText(StringUtils.filterNullAndTrim(item.getTitle()));
            mItemBinding.tvTitle.setText(item.getDescription());
            final List<UploadImage> images = item.getUploadImages();
            if(images.size()<0){
                mItemBinding.gridimageLayout.setVisibility(View.GONE);
                mItemBinding.llGridimage.setVisibility(View.GONE);
            }else {
                final ArrayList<String> urls = new ArrayList<String>();
                for (UploadImage image : images) {
                    urls.add(image.getImageUrl());
                }
                mItemBinding.gridimageLayout.setImage(urls);
            }

        }

    }

    public class VotePlanTextViewHolder extends BaseViewHolder<NewVote>{
        private ItemNewVoteTextBinding mItemBinding;
        public VotePlanTextViewHolder(View convertView) {
            super(convertView);
            mItemBinding = DataBindingUtil.bind(convertView);
        }

        @Override
        protected void onBindView(NewVote item) {
            mItemBinding.tvPlanName.setText(StringUtils.filterNullAndTrim(item.getTitle()));
            mItemBinding.tvTitle.setText(item.getDescription());
        }

    }

}
